/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.calcite.sql.validate;

import static org.apache.calcite.util.Static.RESOURCE;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeField;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlDynamicParam;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.SqlSelect;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * Represents the name-resolution context for expressions in an ORDER BY clause.
 * <p>
 * In some dialects of SQL, the ORDER BY clause can reference column aliases in
 * the SELECT clause. For example, the query
 * </p>
 * <blockquote><code>SELECT empno AS x<br>
 * FROM emp<br>
 * ORDER BY x</code></blockquote>
 * <p>
 * is valid.
 * </p>
 */
public class OrderByScope extends DelegatingScope {

    // ~ Instance fields
    // --------------------------------------------------------

    private final SqlNodeList orderList;
    private final SqlSelect   select;

    // ~ Constructors
    // -----------------------------------------------------------

    OrderByScope(SqlValidatorScope parent, SqlNodeList orderList, SqlSelect select){
        super(parent);
        this.orderList = orderList;
        this.select = select;
    }

    // ~ Methods
    // ----------------------------------------------------------------

    public SqlNode getNode() {
        return orderList;
    }

    public void findAllColumnNames(List<SqlMoniker> result) {
        final SqlValidatorNamespace ns = validator.getNamespace(select);
        addColumnNames(ns, result);
    }

    public SqlQualified fullyQualify(SqlIdentifier identifier) {
        // If it's a simple identifier, look for an alias.
        if (identifier.isSimple() && validator.getConformance().isSortByAlias()) {
            final String name = identifier.names.get(0);
            final SqlValidatorNamespace selectNs = validator.getNamespace(select);
            final RelDataType rowType = selectNs.getRowType();

            final SqlNameMatcher nameMatcher = validator.catalogReader.nameMatcher();
            final RelDataTypeField field = nameMatcher.field(rowType, name);
            final int aliasCount = aliasCount(nameMatcher, name);
            if (aliasCount > 1) {
                // More than one column has this alias.
                throw validator.newValidationError(identifier, RESOURCE.columnAmbiguous(name));
            }
            if (field != null && !field.isDynamicStar() && aliasCount == 1) {
                // if identifier is resolved to a dynamic star, use
                // super.fullyQualify() for such case.
                return SqlQualified.create(this, 1, selectNs, identifier);
            }
        }
        return super.fullyQualify(identifier);
    }

    /**
     * It is not elegant but correct. when aliases of expression is used to
     * order by, Returns the number of columns in the SELECT clause that have
     * {@code name} as their implicit (e.g. {@code t.name}) or explicit (e.g.
     * {@code t.c as name}) alias.
     * <pre>
     * Follow these rules：
     * 1. if the alias is same bug not same column, it is error
     * 2. if the alias is same bug exists at least one expression, use the lastest expression
     * </pre>
     */
    private int aliasCount(SqlNameMatcher nameMatcher, String name) {
        Map<String, Set> map = Maps.newHashMap();
        final String upperCaseKey = name.toUpperCase();
        map.put(upperCaseKey, Sets.newHashSet());
        int n = 0;
        boolean isMatchxpression = false;
        for (SqlNode s : select.getSelectList()) {

            final String alias = SqlValidatorUtil.getAlias(s, -1);
            if (alias != null && nameMatcher.matches(alias, name)) {
                final String newAlias = alias.toUpperCase();
                if (s.getKind() == SqlKind.AS) {
                    if (s instanceof SqlBasicCall) {
                        final SqlNode operand0 = ((SqlBasicCall) s).getOperands()[0];
                        if (operand0 instanceof SqlBasicCall) {
                            isMatchxpression = true;
                            continue;
                        } else if (operand0 instanceof SqlIdentifier) {
                            final String key = operand0.toString().toUpperCase();
                            final Set set = map.get(newAlias);
                            if (set != null) {
                                set.add(key);
                            }
                        } else if (operand0 instanceof SqlDynamicParam) {
                            isMatchxpression = true;
                            continue;
                        } else {
                            isMatchxpression = true;
                            continue;
                        }
                    } else {
                        isMatchxpression = true;
                        continue;
                    }
                } else if (s instanceof SqlIdentifier) {
                    final String key = s.toString().toUpperCase();
                    final Set set = map.get(newAlias);
                    if (set != null) {
                        set.add(key);
                    }
                } else {
                    n++;
                }

            }

        }
        n += map.get(upperCaseKey).size();
        if (isMatchxpression) {
            if (n == 0) {
                n = 1;
            }
        }
        return n;
    }

    public RelDataType resolveColumn(String name, SqlNode ctx) {
        final SqlValidatorNamespace selectNs = validator.getNamespace(select);
        final RelDataType rowType = selectNs.getRowType();
        final SqlNameMatcher nameMatcher = validator.catalogReader.nameMatcher();
        final RelDataTypeField field = nameMatcher.field(rowType, name);
        if (field != null) {
            return field.getType();
        }
        final SqlValidatorScope selectScope = validator.getSelectScope(select);
        return selectScope.resolveColumn(name, ctx);
    }

    public void validateExpr(SqlNode expr) {
        SqlNode expanded = validator.expandOrderExpr(select, expr);

        // expression needs to be valid in parent scope too
        parent.validateExpr(expanded);
    }
}

// End OrderByScope.java
